package com.adjy.pathbuilders.screens;

import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.MotionEvent;

import com.adjy.pathbuilders.R;
import com.adjy.pathbuilders.commons.FontLoader;
import com.adjy.pathbuilders.commons.FontLoader.Fonts;
import com.adjy.pathbuilders.objects.button.Button;
import com.adjy.pathbuilders.objects.button.PaintButton;
import com.adjy.pathbuilders.objects.button.listeners.BackToMainMenuButtonListener;
import com.adjy.pathbuilders.rendering.GameCanvas;
import com.adjy.pathbuilders.rendering.loading.AnimationFactory;
import com.adjy.pathbuilders.state.Game;

public class ClearingScreen implements GameScreen {

	private Button exitButton;
	
	private Paint rectPaint;
	private Paint textPaintButton;
	
	private Paint textPaint;
	private Paint stroke;
	
	private static final int exitButtonX = 400;
	private static final int exitButtonY = 400;
	private static final int exitButtonWidth = 200;
	private static final int exitButtonHeight = 50; 

	private float timeAnimation;
	private static final float timeFirstAnimation = 0;
	private static final float timeToChangeAnimation = 5;
	
	private AnimationFactory factory;
	private Game game;
	
	public ClearingScreen(AnimationFactory factory, Game game) {
		this.game = game;
		textPaint = new Paint();
		textPaint.setAntiAlias(true);
		textPaint.setTextSize(25);
		textPaint.setARGB(255, 172, 51, 22);
		textPaint.setFakeBoldText(true);
		textPaint.setTypeface(FontLoader.getInstance().getFont(Fonts.HOBO));
		
		stroke = new Paint();
		stroke.setStyle(Style.STROKE);
		stroke.setStrokeWidth(1);
		stroke.setTextSize(25);
		stroke.setColor(Color.BLACK);
		stroke.setTypeface(FontLoader.getInstance().getFont(Fonts.HOBO));		
		
		rectPaint = new Paint();
		rectPaint.setColor(Color.WHITE);
		rectPaint.setAntiAlias(true);
		rectPaint.setFakeBoldText(true);
		
		textPaintButton = new Paint();
		textPaintButton.setColor(Color.BLACK);
		textPaintButton.setAntiAlias(true);
		textPaintButton.setFakeBoldText(true);
		
		exitButton = new PaintButton(exitButtonX, exitButtonY, exitButtonWidth, exitButtonHeight,
							new BackToMainMenuButtonListener(game), game.getContext().getString(R.string.back_to_main_menu), 
							rectPaint, textPaintButton);
		
		timeAnimation = 0;
		
		this.factory = factory;
	}
	
	@Override
	public void load() {
		factory.loadImages("main_menu_bg");
	}

	@Override
	public void release() {
		factory.releaseImages("main_menu_bg");
	}

	@Override
	public void input(MotionEvent event) {
		exitButton.input(event);
	}

	@Override
	public void render(GameCanvas mcanvas, float timeElapsed) {
		mcanvas.drawBackground(factory.getImage("main_menu_bg"));
		
		if (timeAnimation >= timeFirstAnimation + 0*timeToChangeAnimation) {
			
			mcanvas.drawTextWithConversion(game.getContext().getString(R.string.congratulations), 200, 200, textPaint);
			mcanvas.drawTextWithConversion(game.getContext().getString(R.string.congratulations), 200, 200, stroke);		
		}
		
		if (timeAnimation >= timeFirstAnimation + 1*timeToChangeAnimation) {
			mcanvas.drawTextWithConversion(game.getContext().getString(R.string.cleared_the_game), 200, 250, textPaint);			
			mcanvas.drawTextWithConversion(game.getContext().getString(R.string.cleared_the_game), 200, 250, stroke);
		}
		
		if (timeAnimation >= timeFirstAnimation + 2*timeToChangeAnimation) {
			mcanvas.drawTextWithConversion(game.getContext().getString(R.string.dare_finish_faster), 200, 300, textPaint);			
			mcanvas.drawTextWithConversion(game.getContext().getString(R.string.dare_finish_faster), 200, 300, stroke);
		}
		
		if (timeAnimation <= timeFirstAnimation + 5*timeToChangeAnimation) {
			timeAnimation += timeElapsed;			
		}
		
		exitButton.render(mcanvas, timeElapsed);
	}

	@Override
	public void step(float timePassed) {
		// TODO Auto-generated method stub

	}
	
	public void reset() {
		timeAnimation = 0;
	}
}
